package com.example.drainage.ui.media.util

import android.app.ActivityManager
import android.app.Application
import android.content.Context
import android.os.Build
import android.os.Process

const val KEY_PLAY_AUDIO_SWITCH = "KEY_PLAY_AUDIO_SWITCH"
const val KEY_PLAY_AUDIO_PATH = "KEY_PLAY_AUDIO_PATH"
const val KEY_PLAY_AUDIO_URL = "KEY_PLAY_AUDIO_URL"
const val KEY_PLAY_AUDIO_BROADCAST = "com.audio.play.controller"

fun isMainProcess(context: Context): Boolean {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        context.packageName.equals(Application.getProcessName())
    } else {
        context.packageName.equals(getProcessName(context))
    }
}

fun getProcessName(context: Context): String {
    try {
        val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val list = activityManager.runningAppProcesses
        val iterator = list.iterator()
        while (iterator.hasNext()) {
            val processInfo = iterator.next()
            if (null != processInfo && processInfo.pid == Process.myPid()) {
                return processInfo.processName
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return "unknown"
}